/**
 * 权限相关的数据
 * Created by gcc on 2017/9/29.
 */

import Vue from "vue";


// 页码相关
let page = {
  total: 1,
  startIndex: 0,
  stepNumber: 40,
  currentPage: 1
};

let searchInfoPage ={
  total: 1,
  startIndex: 0,
  stepNumber: 40,
  currentPage: 1
};

//页面状态相关
let status = {
  //显示order的 modal
  modalOrder: {
    isShow: false,
  },
  //显示cart的 modal
  modalCart: {
    isShow: false,
  },
  //显示address的 modal
  modalAddress: {
    isShow: false,
  },
  //显示record的 modal
  modalRecord: {
    isShow: false,
  },
  //查看图片
  modalImage: {
    isShow: false,
    url: ''
  },
  modalUserInfo: {
    isShow: false
  },
  showCouponEnable:false,
  modalDistuibuteCoupon: {
    isShow: false,
    couponDefines:[]
  },
  modalAssignUser:{
    isShow: false
  }

};

// 和网络交互相关的参数
let params = {
  tabId:'',
  //搜索条件
  userName: '',
  encodeName:'',
  startTime:"",
  endTime:"",
  orderBy: '',
  showOrder: {
    userId: '',
    pageNum: 1,
    pageSize: 40,
    total: 1
  },
  showCart: {
    userId: ''
  },
  showAddress: {
    userId: ''
  },
  showRecord: {
    userId: '',
    pageNum: 1,
    pageSize: 20,
    total: 1
  },
  showUserInfo: {
    firstName: '',
    lastName: '',
    thirdParty: '',
    state: '',
    country: '',
    phone: '',
    email: '',
    isPushMessage: '',
    isAdEmail: '',
    facebook: '',
    whatsapp: '',
    twitter: '',
    instagram: '',
    source: '',
    inviteCode: '',
    useContent: '',
    hasEmployee : false,
    userId:'',
    tabList:[],
  },
  selectedUsers:[],
  selectedCoupon:{},
  assignUser:{
    userId : "",
    userName :"",
    employeeId : "",
    isCurrentEmployee: true
  }

};

// 页面的数据源
let data = {
  userList: [],
  tabList:[],
  loginTypeDict: [],
  orderItemDict: [],
  orderList: [],
  cartList: [],
  recordList: [],
  cartWeightCount: '',
  cartPriceCount: '',

  dateRange: {
    disabledDate(date) {
      return date && date.valueOf() > Date.now() - 86400000;
    },
    shortcuts: [
      {
        text: '清空时间',
        value() {
          return [];
        }
      },
      {
        text: '最近一周',
        value() {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
          return [start, end];
        }
      },
      {
        text: '最近一个月',
        value() {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
          return [start, end];
        }
      },
      {
        text: '最近三个月',
        value() {
          const end = new Date();
          const start = new Date();
          start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
          return [start, end];
        }
      }
    ]
  }
};

let tableTitles = function (self) {
  let titles =  [
    {
      type: 'selection',
      align: 'center',
      fixed: 'left',
      width: 60
    },
    {
      type: 'index',
      title: '#',
      width: 60,
      key: 'index',
      fixed: 'left',
      align: 'center',
    },
     {
      title: '头像',
      key: 'avatar',
      width:  80,
      align: 'center',
      render: (h, params) => {
        return (
          <div>
            <img src={params.row.avatar} height={50} onClick={()=>{
              status.modalImage.url = params.row.avatar;
              status.modalImage.isShow = true;
            }}></img>
          </div>
        );
      }
    } ,
    {
      title: '用户名',
      key: 'userName',
      width: 150,
      fixed: 'left',
      render: (h, params) => {
        // let hiddenName = Vue.prototype.$tiyHideUsername(params.row.fullName);
        let hiddenName = params.row.encodeName;
        if(!self.$authority('USER-1-210')){
          return (<span>{hiddenName}</span>);
        }else {
          return (
            <div>
              <a style="padding:6px;font-size:13px" onClick={() => {self.showUserInfo(params.index);}}>
          {hiddenName}
              </a>
            </div>
          );
        }
      }
    },
    !self.$authority("USER-1-202")? null : {
      title: '国家',
      key: 'country',
      width: 80,
      align: 'center'
    },
    {
      title: '注册方式',
      key: 'thirdParty',
      width: 100,
      align: 'center',
      render: (h, params) => {
        var way = '邮箱登录';
        data.loginTypeDict.forEach(function (item) {
          if (item.value === params.row.thirdParty) {
            way = item.name;
          }
        });
        return (
          <span style="fontSize: 12px">{way}</span>
        );
      }
    },
    {
      title: '注册时间',
      key: 'registertime',
      minWidth: 150,
      align: 'center',
      render: (h, params) => {
        return h('div', self.$tiyBJTime(params.row.registertime));
      }
    },
    {
      title: '标签',
      key: 'userTabs',
      minWidth: 140,
      align: 'center',
      render: (h, params) => {
        if(params.row.tabsName){
          return (<tag color="red">{params.row.tabsName}</tag>);
        } else {
          return (<div></div>);
        }
      }
    },
    !self.$authority("USER-1-205")? null : {
      title: '订单',
      width: 90,
      key: 'user_order_count',
      align: 'center',
      sortable: 'custom',
      render: (h, params) => {
        if(!self.$authority('USER-1-211')){
          return h('span', params.row.userOrderCount);
        }else{
          return (
            <div>
              <a style="padding:6px;font-size:0.9rem;color:#448aff;font-weight:600" onClick={() => {
                if (params.row.userOrderCount !== 0) {
                  self.showOrder(params.index);
                }
              }}>
                {params.row.userOrderCount}
              </a>
            </div>
          );
        }
      }
    },
    !self.$authority("USER-1-206")? null :{
      title: '购物车',
      width: 100,
      key: 'user_cart_count',
      align: 'center',
      sortable: 'custom',
      render: (h, params) => {

        if(!self.$authority('USER-1-212')){
          return h('span',params.row.userCartCount);
        }else{
          return (
            <div>
              <a style="padding:6px;font-size:0.9rem;color:#448aff;font-weight:600" onClick={() => {
                if (params.row.userCartCount !== 0) {
                  self.showCart(params.index);
                }
              }}>
                {params.row.userCartCount}
              </a>
            </div>
          );
        }
      }
    },
    {
      title: '状态',
      key: 'state',
      width: 100,
      render: (h, params) => {
        var stateStr = '';
        self.$store.getters.dictionaries.userStateDict.forEach(function (item) {
          if (item.value === params.row.state) {
            stateStr = item.name;
            return;
          }
        });
        var isActived = params.row.state ===110;
        //110 未激活
        return(
          <div>
            <badge status={isActived?'default':'success'} text={stateStr}/>
          </div>
        );
        // return h('div', stateStr);
      }
    },
    !self.$authority("USER-1-209")? null : {
      title: '登录记录',
      width: 100,
      key: 'record',
      align: 'center',
      render: (h, params) => {
        return (
          <div>
            < i-button type="primary" size="small"   onClick={() => {  self.showRecord(params.index);}}>查看</i-button>
          </div>
        );
      }
    },
    !self.$authority("USER-1-214")? null : {
      title: '发送邮件',
      width: 100,
      align: 'center',
      render: (h, params) => {
        return (
          <div>
            < i-button type="primary" size="small"   onClick={() => {  self.showEmail(params.index);}}>发送</i-button>
          </div>
        );
      }
    }
  ] ;
  return self.$authTableTitles(titles);
};
let tableOrderTitles = function(self){
  return [
    {
      type: 'index',
      title: '#',
      width: 80,
      align: 'center'
    },
    {
      title: '订单号',
      key: "serialNumber",
      align: 'center',
      width: 200
    },
    {
      title: '国家',
      key: 'countryName',
      align: 'center',
      width: 100
    },
    {
      title: '金额($)',
      key: 'actualPayment',
      align: 'center',
      width: 160
    },
    {
      title: '下单时间',
      align: 'center',
      width: 200,
      render: (h, params) => {
        return h('div', self.$tiyBJTime(params.row.createtime));
      }
    },
    {
      title: '订单状态',
      align: 'center',
      render: (h, params) => {
        var state = '';
        data.orderItemDict.forEach(function (item) {
          if (params.row.state === item.value) {
            state = item.name;
          }
        });
        return h('div', state);
      }
    },
  ];
}
let tableCartTitles = function(self) {
  return [
    {
      title: '产品编码',
      key: 'productCode',
      align: 'center'
    },
    {
      title: '产品图片',
      key: 'colorJson',
      align: 'center',
      render: (h, params) => {
        var image = params.row.productImgUrl;
        let yunPath = Vue.prototype.$store.getters.imgPrefix;
        return h('div', [h('img', {
          attrs: {src: yunPath + image, height: 60},
          on: {
            click: () => {
              status.modalImage.url = yunPath + image;
              status.modalImage.isShow = true;
            }
          }
        }, image)
        ]);
      }
    },
    {
      title: '色卡',
      key: 'color',
      align: 'center',
    },
    {
      title: '尺码规格',
      key: 'sizeName',
      align: 'center',
    },
    {
      title: '当前售价',
      align: 'center',
      key:'price',
      render: (h, params) => {
        return h('div',"$"+params.row.price);
      }
    },
    {
      title: '	数量',
      key: 'amount',
      align: 'center',
    },
    {
      title: '重量',
      key: 'weight',
      align: 'center',
      render: (h, params) => {
        return h('div',params.row.weight+"g");
      }
    },
    {
      title: '添加时间',
      key: 'createtime',
      align: 'center',
      width: 150,
      render: (h, params) => {
        return h('div', self.$tiyBJTime(params.row.createtime));
      }
    }
  ];
};
let tableAddressTitles = [
  {
    type: 'index',
    title: '#',
    width: 50
  },
  {
    title: '收货人',
    key: 'receiver',
    width: 100
  },
  {
    title: '电话',
    key: 'phone',
    width: 100
  },
  {
    title: '邮政编码',
    key: 'postcode',
    width: 100
  },
  {
    title: '国家',
    key: 'country',
    width: 100
  },
  {
    title: '地址',
    render: (h, params) => {
      return h('div', params.row.state + '|' + params.row.city + '|' + params.row.houseNumber);
    }
  }
];

let tableRecordTitles = function(self) {
  return [
    {
      type: 'index',
      title: '#',
      width: 80,
      align: 'center'
    },
    {
      title: '用户名',
      key: 'userName',
      width: 150,
      align: 'center',
      render: (h, params) => {
        var encodeName = params.row.encodeName;
        if(encodeName==''){
          encodeName = params.row.userName;
        }
        return h('div', encodeName);
      }
    },
    {
      title: '登录时间',
      key: 'optTime',
      align: 'center',
      render: (h, params) => {
        return h('div', self.$tiyBJTime(params.row.optTime));
      }
    },
    {
      title: 'app版本',
      key: 'version',
      align: 'center',
      render: (h, params) => {
        return h('div', params.row.version===0?'':params.row.version );
      }
    },
    {
      title: '登录方式',
      key: 'loginOpt',
      align: 'center',
      render: (h, params) => {
        var way = '';
        data.loginTypeDict.forEach(function (item) {
          if (item.value === params.row.loginOpt) {
            way = item.name;
          }
        });
        return h('div', way);
      }
    },
    {
      title: '终端类型',
      key: 'deviceType',
      align: 'center',
      render: (h, params) => {
        let device = '';
        switch(params.row.deviceType){
          case 1:
            device = 'Android';
            break;
          case 2:
            device = 'IOS';
            break;
          case 3:
            device = 'PC';
            break;
          case 4:
            device = 'Mobile PC';
            break;
          default:
            break;
        }
        return h('div', device);
      }
    },
    {
      title: '客户端IP',
      key: 'clientIp',
      align: 'center',
      render: (h, params) => {
        return h('div', params.row.clientIp);
      }
    },
  ]
};



export default {
  tPage: page,
  searchInfoPage: searchInfoPage,
  tStatus: status,
  tParams: params,
  userTableTitles: tableTitles,
  recordTableTitles: tableRecordTitles,
  orderTableTitles: tableOrderTitles,
  cartTableTitles: tableCartTitles,
  addressTableTitles: tableAddressTitles,
  tData: data,
  tRole: {}
};
